//
// Created by gogoqiu on 2022/2/14.
//

#include "Printout.h"

bool Printout::OnBeginDocument(int startPage, int endPage)
{
    if (!wxPrintout::OnBeginDocument(startPage, endPage))
        return false;

    return true;
}

void Printout::GetPageInfo(int *minPage, int *maxPage,int *pageFrom, int *pageTo)
{
    *minPage = 1;
    *maxPage = 2;
    *pageFrom = 1;
    *pageTo = 2;
}

bool Printout::HasPage(int pageNum)
{
    return (pageNum >= 1 && pageNum <= 2);
}

void Printout::DrawPageOne(wxDC*dc)
{
    // 下面的代码可以这样写只是因为我们知道图片的大小是200 x200
    // 如果我们不知道的话，需要先计算图片的大小
    float maxX = 200;
    float maxY = 200;
    // 让我们先设置至少个设备单位的边框50
    float marginX = 50;
    float marginY = 50;
    // 将边框的大小增加到图片的周围
    maxX += (2*marginX );
    maxY += (2*marginY );
    // 获取象素单位的当前设备上下文的大小
    int w, h;
    dc->GetSize (&w, &h);
    //计算一个合适的缩放值
    float scaleX =( float )(w/ maxX );
    float scaleY =( float )(h/ maxY );
    // 选择或者方向上较小的那个XY
    float actualScale = wxMin ( scaleX , scaleY );
    // 计算图片在设备上的合适位置以便居中
    float posX = ( float )(( w*(200*actualScale ))/2.0);
    float posY = ( float )(( h*(200*actualScale ))/2.0);
    // 设置设备平移和缩放
    dc->SetUserScale ( actualScale , actualScale );
    dc->SetDeviceOrigin ( ( long )posX , ( long ) posY );
    // ok现在开始画画
    dc->SetBackground(*wxWHITE_BRUSH );
    dc->Clear();
//    dc->SetFont ( wxGetApp ().m_testFont );
    dc->SetBackgroundMode ( wxTRANSPARENT );
    dc->SetBrush (*wxCYAN_BRUSH );
    dc->SetPen (*wxRED_PEN );
    dc->DrawRectangle (0, 30, 200 , 100);
    dc->DrawText ( wxT (" Rectangle 200 by 100 "), 40, 40);
    dc->SetPen ( wxPen (*wxBLACK ,0, wxDOT_DASH ) );
    dc->DrawEllipse (50 , 140 , 100 , 50);
    dc->SetPen (*wxRED_PEN );
    dc->DrawText ( wxT (" Test message : this is in 10 point text "),10, 180);
}

void Printout::DrawPageTwo(wxDC *dc)
{
    int ppiScreenX, ppiScreenY;
    GetPPIScreen(&ppiScreenX, &ppiScreenY);
    int ppiPrinterX, ppiPrinterY;
    GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
    float scale = (float )((float )ppiPrinterX/(float)ppiScreenX);
    int pageWidth, pageHeight;
    int w, h;
    dc->GetSize(&w, &h);
    GetPageSizePixels(&pageWidth, &pageHeight);
    float overallScale = scale*( float )(w/(float)pageWidth);
    dc->SetUserScale(overallScale, overallScale) ;
    float logUnitsFactor = (float )(ppiPrinterX/(scale*25.4));
    float logUnits = (float)(50*logUnitsFactor);
    dc->SetPen(*wxBLACK_PEN);
    dc->DrawLine(50, 250, (long)(50.0 + logUnits), 250);
    dc->DrawLine(50, 250, 50, (long)(250.0 + logUnits));
    dc->SetBackgroundMode(wxTRANSPARENT);
    dc->SetBrush(*wxTRANSPARENT_BRUSH);
//    dc->SetFont(wxGetApp().m_testFont);
    dc->DrawText(wxT("Some test text"), 200, 300 );
}

bool Printout::OnPrintPage(int page)
{
    wxDC *dc = GetDC();
    if (dc)
    {
        if (page == 1)
        {
            DrawPageOne(dc);
        }

        else if (page == 2)
        {
            DrawPageTwo(dc);
        }
        MapScreenSizeToPage();
        dc->DrawText(wxString::Format(wxT("PAGE %d"), page), 0, 0);
        return true;
    }
    else
        return false;
}